﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core;
using COFE.Core.Profile;

namespace COFE.Helpers
{
    public static class COFEUser
    {

        #region Methods

        private static IUserProfileManager2 checkUpm()
        {
            return COFEAdmin.checkUpm();
        }

        public static bool Login(string userName, string password)
        {
            var upm = checkUpm();
            if (upm != null)
                return upm.Login(userName, password) != null;
            else return true;
        }

        public static void Logout()
        {
            var upm = checkUpm();
            if (upm != null)
            {
                var up = COFEShared.CurrentUserProfile as IUserProfile2;
                if (up != null)
                    upm.Logout(up.AuthorizationKey);
                else upm.SetCurrentUserProfile(null);
            }
        }

        #endregion

        #region Public Property

        public static IUserProfile2 UserProfile
        {
            get
            {
                return COFEShared.CurrentUserProfile is IUserProfile2 ?
                  COFEShared.CurrentUserProfile as IUserProfile2 : null;
            }
        }

        public static string UserName
        {
            get
            {
                return COFEShared.CurrentUserProfile != null ?
                    COFEShared.CurrentUserProfile.UserName : null;
            }
        }

        public static Guid AuthorizationKey
        {
            get
            {
                return UserProfile != null ? UserProfile.AuthorizationKey : Guid.Empty;
            }
        }



        #endregion

    }
}
